<!DOCTYPE html>
<html prefix="og: http://ogp.me/ns# article: http://ogp.me/ns/article# " lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>7-dimensionality-reduction-with-pca | 绿萝间</title>
<link href="../assets/css/all-nocdn.css" rel="stylesheet" type="text/css">
<link href="../assets/css/ipython.min.css" rel="stylesheet" type="text/css">
<link href="../assets/css/nikola_ipython.css" rel="stylesheet" type="text/css">
<meta name="theme-color" content="#5670d4">
<meta name="generator" content="Nikola (getnikola.com)">
<link rel="alternate" type="application/rss+xml" title="RSS" href="../rss.xml">
<link rel="canonical" href="https://muxuezi.github.io/posts/7-dimensionality-reduction-with-pca.html">
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"] ],
        displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
        processEscapes: true
    },
    displayAlign: 'center', // Change this to 'center' to center equations.
    "HTML-CSS": {
        styles: {'.MathJax_Display': {"margin": 0}}
    }
});
</script><!--[if lt IE 9]><script src="../assets/js/html5.js"></script><![endif]--><meta name="author" content="Tao Junjie">
<link rel="prev" href="dlott-15075-2015-07-01-report.html" title="大乐透15075期(2015-07-01)数据分析报告" type="text/html">
<link rel="next" href="slott-2015076-2015-07-02-report.html" title="双色球2015076期(2015-07-02)数据分析报告" type="text/html">
<meta property="og:site_name" content="绿萝间">
<meta property="og:title" content="7-dimensionality-reduction-with-pca">
<meta property="og:url" content="https://muxuezi.github.io/posts/7-dimensionality-reduction-with-pca.html">
<meta property="og:description" content="用PCA降维¶








本章我们将介绍一种降维方法，PCA（Principal Component Analysis，主成分分析）。降维致力于解决三类问题。第一，降维可以缓解维度灾难问题。第二，降维可以在压缩数据的同时让信息损失最小化。第三，理解几百个维度的数据结构很困难，两三个维度的数据通过可视化更容易理解。下面，我们用PCA将一个高维数据降成二维，方便可视化，之后，我们建一个脸部识别系">
<meta property="og:type" content="article">
<meta property="article:published_time" content="2015-07-02T09:38:42+08:00">
<meta property="article:tag" content="CHS">
<meta property="article:tag" content="ipython">
<meta property="article:tag" content="Machine Learning">
<meta property="article:tag" content="Python">
<meta property="article:tag" content="scikit-learn tutorial">
</head>
<body>
<a href="#content" class="sr-only sr-only-focusable">Skip to main content</a>

<!-- Menubar -->

<nav class="navbar navbar-inverse navbar-static-top"><div class="container">
<!-- This keeps the margins nice -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-navbar" aria-controls="bs-navbar" aria-expanded="false">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="https://muxuezi.github.io/">

                <span id="blog-title">绿萝间</span>
            </a>
        </div>
<!-- /.navbar-header -->
        <div class="collapse navbar-collapse" id="bs-navbar" aria-expanded="false">
            <ul class="nav navbar-nav">
<li>
<a href="../archive.html">Archive</a>
                </li>
<li>
<a href="../categories/">Tags</a>
                </li>
<li>
<a href="../rss.xml">RSS feed</a>

                
            </li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
    <a href="7-dimensionality-reduction-with-pca.ipynb" id="sourcelink">Source</a>
    </li>

                
            </ul>
</div>
<!-- /.navbar-collapse -->
    </div>
<!-- /.container -->
</nav><!-- End of Menubar --><div class="container" id="content" role="main">
    <div class="body-content">
        <!--Body content-->
        <div class="row">
            
            
<article class="post-text h-entry hentry postpage" itemscope="itemscope" itemtype="http://schema.org/Article"><header><h1 class="p-name entry-title" itemprop="headline name"><a href="#" class="u-url">7-dimensionality-reduction-with-pca</a></h1>

        <div class="metadata">
            <p class="byline author vcard"><span class="byline-name fn">
                    Tao Junjie
            </span></p>
            <p class="dateline"><a href="#" rel="bookmark"><time class="published dt-published" datetime="2015-07-02T09:38:42+08:00" itemprop="datePublished" title="2015-07-02 09:38">2015-07-02 09:38</time></a></p>
            
        <p class="sourceline"><a href="7-dimensionality-reduction-with-pca.ipynb" id="sourcelink">Source</a></p>

        </div>
        

    </header><div class="e-content entry-content" itemprop="articleBody text">
    <div tabindex="-1" id="notebook" class="border-box-sizing">
    <div class="container" id="notebook-container">

<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="用PCA降维">用PCA降维<a class="anchor-link" href="7-dimensionality-reduction-with-pca.html#%E7%94%A8PCA%E9%99%8D%E7%BB%B4">¶</a>
</h2>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>本章我们将介绍一种降维方法，PCA（Principal Component Analysis，主成分分析）。降维致力于解决三类问题。第一，降维可以缓解维度灾难问题。第二，降维可以在压缩数据的同时让信息损失最小化。第三，理解几百个维度的数据结构很困难，两三个维度的数据通过可视化更容易理解。下面，我们用PCA将一个高维数据降成二维，方便可视化，之后，我们建一个脸部识别系统。</p>
<!-- TEASER_END-->
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="PCA简介">PCA简介<a class="anchor-link" href="7-dimensionality-reduction-with-pca.html#PCA%E7%AE%80%E4%BB%8B">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>在<em>第三章，特征提取与处理</em>里面，涉及高维特征向量的问题往往容易陷入维度灾难。随着数据集维度的增加，算法学习需要的样本数量呈指数级增加。有些应用中，遇到这样的大数据是非常不利的，而且从大数据集中学习需要更多的内存和处理能力。另外，随着维度的增加，数据的稀疏性会越来越高。在高维向量空间中探索同样的数据集比在同样稀疏的数据集中探索更加困难。</p>
<p>主成分分析也称为卡尔胡宁-勒夫变换（Karhunen-Loeve Transform），是一种用于探索高维数据结构的技术。PCA通常用于高维数据集的探索与可视化。还可以用于数据压缩，数据预处理等。PCA可以把可能具有相关性的高维变量合成线性无关的低维变量，称为主成分（ principal components）。新的低维数据集会经可能的保留原始数据的变量。</p>
<p>PCA将数据投射到一个低维子空间实现降维。例如，二维数据集降维就是把点投射成一条线，数据集的每个样本都可以用一个值表示，不需要两个值。三维数据集可以降成二维，就是把变量映射成一个平面。一般情况下，$n$维数据集可以通过映射降成$k$维子空间，其中$k<n><p>假如你是一本养花工具宣传册的摄影师，你正在拍摄一个水壶。水壶是三维的，但是照片是二维的，为了更全面的把水壶展示给客户，你需要从不同角度拍几张图片。下图是你从四个方向拍的照片：</p>
<p><img src="mlslpic/7.1%20wateringcan.png" alt="wateringcan"></p>
<p>第一张图里水壶的背面可以看到，但是看不到前面。第二张图是拍前面，可以看到壶嘴，这张图可以提供了第一张图缺失的信息，但是壶把看不到了。从第三张俯视图里无法看出壶的高度。第四张图是你打算放进目录的，水壶的高度，顶部，壶嘴和壶把都清晰可见。</p>
<p>PCA的设计理念与此类似，它可以将高维数据集映射到低维空间的同时，尽可能的保留更多变量。PCA旋转数据集与其主成分对齐，将最多的变量保留到第一主成分中。假设我们有下图所示的数据集：</p>
<p><img src="mlslpic/7.2%20dataset.png" alt="dataset"></p>
<p>数据集看起来像一个从原点到右上角延伸的细长扁平的椭圆。要降低整个数据集的维度，我们必须把点映射成一条线。下图中的两条线都是数据集可以映射的，映射到哪条线样本变化最大？</p>
<p><img src="mlslpic/7.3%20datasetline.png" alt="datasetline"></p>
<p>显然，样本映射到虚线的变化比映射到点线的变化。实际上，这条虚线就是第一主成分。第二主成分必须与第一主成分正交，也就是说第二主成分必须是在统计学上独立的，会出现在与第一主成分垂直的方向，如下图所示：</p>
<p><img src="mlslpic/7.4%20orthogonal.png" alt="orthogonal"></p>
<p>后面的每个主成分也会尽量多的保留剩下的变量，唯一的要求就是每一个主成分需要和前面的主成分正交。</p>
<p>现在假设数据集是三维的，散点图看起来像是沿着一个轴旋转的光盘。</p>
<p><img src="mlslpic/7.5%20threedimensional.png" alt="threedimensional"></p>
<p>这些点可以通过旋转和变换使光盘完全变成二维的。现在这些点看着像一个椭圆，第三维上基本没有变量，可以被忽略。</p>
<p>当数据集不同维度上的方差分布不均匀的时候，PCA最有用。如果是一个球壳行数据集，PCA不能有效的发挥作用，因为各个方向上的方差都相等；没有丢失大量的信息维度一个都不能忽略。</p>

</n></p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="PCA计算步骤">PCA计算步骤<a class="anchor-link" href="7-dimensionality-reduction-with-pca.html#PCA%E8%AE%A1%E7%AE%97%E6%AD%A5%E9%AA%A4">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>在介绍PCA的运行步骤之前，有一些术语需要说明一下。</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="方差，协方差和协方差矩阵">方差，协方差和协方差矩阵<a class="anchor-link" href="7-dimensionality-reduction-with-pca.html#%E6%96%B9%E5%B7%AE%EF%BC%8C%E5%8D%8F%E6%96%B9%E5%B7%AE%E5%92%8C%E5%8D%8F%E6%96%B9%E5%B7%AE%E7%9F%A9%E9%98%B5">¶</a>
</h4>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>方差（Variance）是度量一组数据分散的程度。方差是各个样本与样本均值的差的平方和的均值：</p>
$$s^2 = \frac {\sum_{i=1}^n {{(X_i-\bar X)}^2}} {n-1}$$<p>协方差（Covariance）是度量两个变量的变动的同步程度，也就是度量两个变量线性相关性程度。如果两个变量的协方差为0，则统计学上认为二者线性无关。注意两个无关的变量并非完全独立，只是没有线性相关性而已。计算公式如下：</p>
$$cov(X,Y)=\frac {\sum_{i=1}^n {(X_i-\bar X)(X_i-\bar Y)}} {n-1}$$<p>如果协方差不为0，如果大于0表示正相关，小于0表示负相关。当协方差大于0时，一个变量增大是另一个变量也会增大。当协方差小于0时，一个变量增大是另一个变量会减小。协方差矩阵（Covariance matrix）由数据集中两两变量的协方差组成。矩阵的第$(i,j)$个元素是数据集中第$i$和第$j$个元素的协方差。例如，三维数据的协方差矩阵如下所示：</p>
$$
C=
\begin{bmatrix}
cov(x_1,x_1) &amp; cov(x_1,x_2) &amp; cov(x_1,x_3)\\
cov(x_2,x_1) &amp; cov(x_2,x_2) &amp; cov(x_2,x_3)\\
cov(x_3,x_1) &amp; cov(x_3,x_2) &amp; cov(x_3,x_3)\\
\end{bmatrix}
$$<p>让我们计算下表数据的协方差矩阵：</p>
<table>
<thead><tr>
<th style="text-align:center">X1</th>
<th style="text-align:center">X2</th>
<th style="text-align:center">X3</th>
</tr></thead>
<tbody>
<tr>
<td style="text-align:center">2</td>
<td style="text-align:center">0</td>
<td style="text-align:center">−1.4</td>
</tr>
<tr>
<td style="text-align:center">2.2</td>
<td style="text-align:center">0.2</td>
<td style="text-align:center">−1.5</td>
</tr>
<tr>
<td style="text-align:center">2.4</td>
<td style="text-align:center">0.1</td>
<td style="text-align:center">−1</td>
</tr>
<tr>
<td style="text-align:center">1.9</td>
<td style="text-align:center">0</td>
<td style="text-align:center">−1.2</td>
</tr>
</tbody>
</table>
<p>三个变量的样本均值分别是2.125，0.075和-1.275。用Numpy计算协方差矩阵如下：</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [1]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="n">X</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.4</span><span class="p">],</span>
    <span class="p">[</span><span class="mf">2.2</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.5</span><span class="p">],</span>
    <span class="p">[</span><span class="mf">2.4</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">],</span>
    <span class="p">[</span><span class="mf">1.9</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.2</span><span class="p">]]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">cov</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">X</span><span class="p">)</span><span class="o">.</span><span class="n">T</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>[[ 0.04916667  0.01416667  0.01916667]
 [ 0.01416667  0.00916667 -0.00583333]
 [ 0.01916667 -0.00583333  0.04916667]]
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="特征向量和特征值">特征向量和特征值<a class="anchor-link" href="7-dimensionality-reduction-with-pca.html#%E7%89%B9%E5%BE%81%E5%90%91%E9%87%8F%E5%92%8C%E7%89%B9%E5%BE%81%E5%80%BC">¶</a>
</h4>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>向量是具有大小（magnitude）和方向（direction）的几何概念。特征向量（eigenvector）是一个矩阵的满足如下公式的非零向量：</p>
$$A \vec \nu = \lambda \vec \nu$$<p>其中，$\vec \nu$是特征向量，$A$是方阵，$\lambda$是特征值。经过$A$变换之后，特征向量的方向保持不变，只是其大小发生了特征值倍数的变化。也就是说，一个特征向量左乘一个矩阵之后等于等比例放缩（scaling）特征向量。德语单词<em>eigen</em>的意思是<em>属于...或...专有（ belonging to or peculiar to）</em>；矩阵的特征向量是属于并描述数据集结构的向量。</p>
<p>特征向量和特征值只能由方阵得出，且并非所有方阵都有特征向量和特征值。如果一个矩阵有特征向量和特征值，那么它的每个维度都有一对特征向量和特征值。矩阵的主成分是其协方差矩阵的特征向量，按照对应的特征值大小排序。最大的特征值就是第一主成分，第二大的特征值就是第二主成分，以此类推。</p>
<p>让我们来计算下面矩阵的特征向量和特征值：</p>
$$A=
\begin{bmatrix}
1 &amp; -2 \\
2 &amp; -3 \\
\end{bmatrix}
$$<p>根据前面的公式$A$乘以特征向量，必然等于特征值乘以特征向量。我们建立特征方程求解：</p>
$$(A- \lambda I) \vec \nu=0$$$$|A-\lambda * I| = 
\begin{vmatrix}
\begin{bmatrix}
1 &amp; -2 \\
2 &amp; -3 \\
\end{bmatrix}
-
\begin{bmatrix}
\lambda &amp; 0 \\
0 &amp; \lambda \\
\end{bmatrix}
\end{vmatrix}
= 0
$$<p>从特征方程可以看出，矩阵与单位矩阵和特征值乘积的矩阵行列式为0：</p>
$$
\begin{vmatrix}
\begin{bmatrix}
1-\lambda &amp; -2 \\
2 &amp; -3-\lambda \\
\end{bmatrix}
\end{vmatrix}
=(\lambda+1)(\lambda+1)
= 0
$$<p>矩阵的两个特征值都等于-1。现在再用特征值来解特征向量。</p>
$$A \vec \nu = \lambda \vec \nu$$<p>首先，我们用特征方程：</p>
$$(A- \lambda I) \vec \nu=0$$<p>把数据带入：</p>
$$
\begin{pmatrix}
\begin{bmatrix}
1 &amp; -2 \\
2 &amp; -3 \\
\end{bmatrix}
-
\begin{bmatrix}
\lambda &amp; 0 \\
0 &amp; \lambda \\
\end{bmatrix}
\end{pmatrix}
\vec \nu
=
\begin{bmatrix}
1-\lambda &amp; -2 \\
2 &amp; -3-\lambda \\
\end{bmatrix}
\vec \nu
=
\begin{bmatrix}
1-\lambda &amp; -2 \\
2 &amp; -3-\lambda \\
\end{bmatrix}
\begin{bmatrix}
\nu_{1,1} \\
\nu_{1,2} \\
\end{bmatrix}
=0
$$
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>我们把特征值代入方程：</p>
$$\begin{bmatrix}
1-(-1) &amp; -2 \\
2 &amp; -3-(-1) \\
\end{bmatrix}
\begin{bmatrix}
\nu_{1,1} \\
\nu_{1,2} \\
\end{bmatrix}
=
\begin{bmatrix}
2 &amp; -2 \\
2 &amp; -2 \\
\end{bmatrix}
\begin{bmatrix}
\nu_{1,1} \\
\nu_{1,2} \\
\end{bmatrix}
=0
$$<p>可以重新整理成如下方程：</p>
<p>\begin{Bmatrix}
2\nu<em>{1,1} + -(2\nu</em>{1,2})=0 \
2\nu<em>{1,1} + -(2\nu</em>{1,2})=0 \
\end{Bmatrix}</p>
<p>任何满足方程的非零向量都可以作为特征向量：
$$
\begin{bmatrix}
1 &amp; -2 \
2 &amp; -3 \
\end{bmatrix}
\begin{bmatrix}
1 \
1 \</p>
<h2 id="\end{bmatrix}">\end{bmatrix}<a class="anchor-link" href="7-dimensionality-reduction-with-pca.html#%5Cend%7Bbmatrix%7D">¶</a>
</h2>\begin{bmatrix}
1 \\
1 \\
\end{bmatrix}<p>=
\begin{bmatrix}
-1 \
-1 \
\end{bmatrix}
$$</p>
<p>PCA需要单位特征向量，也就是L2范数等于1的特征向量：</p>
$$
\begin{Vmatrix}
x \\
\end{Vmatrix}
=
\sqrt {x_1^2 + x_2^2 + \dots + x_n^2}
$$<p>那么把前面的特征向量带入可得：</p>
$$
\begin{Vmatrix}
\begin{bmatrix}
1 \\
1 \\
\end{bmatrix}
\end{Vmatrix}
=
\sqrt {1^2+1^2}
=
\sqrt 2
$$<p>于是单位特征向量是：</p>
$$
\begin{bmatrix}
1 \\
1 \\
\end{bmatrix} / {\sqrt 2}
=
\begin{bmatrix}
0.70710678 \\
0.70710678 \\
\end{bmatrix}
$$<p>我们可以通过Numpy检验我们手算的特征向量。<code>eig</code>函数返回特征值和特征向量的元组：</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [8]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="n">w</span><span class="p">,</span> <span class="n">v</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">eig</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">]]))</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">'特征值：</span><span class="si">{}</span><span class="se">\n</span><span class="s1">特征向量：</span><span class="si">{}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">w</span><span class="p">,</span><span class="n">v</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>特征值：[-0.99999998 -1.00000002]
特征向量：[[ 0.70710678  0.70710678]
 [ 0.70710678  0.70710678]]
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="用PCA降维">用PCA降维<a class="anchor-link" href="7-dimensionality-reduction-with-pca.html#%E7%94%A8PCA%E9%99%8D%E7%BB%B4">¶</a>
</h4>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>让我们用PCA方法把下表二维数据降成一维：</p>
<table>
<thead><tr>
<th style="text-align:center">X1</th>
<th style="text-align:center">X2</th>
</tr></thead>
<tbody>
<tr>
<td style="text-align:center">0.9</td>
<td style="text-align:center">1</td>
</tr>
<tr>
<td style="text-align:center">2.4</td>
<td style="text-align:center">2.6</td>
</tr>
<tr>
<td style="text-align:center">1.2</td>
<td style="text-align:center">1.7</td>
</tr>
<tr>
<td style="text-align:center">0.5</td>
<td style="text-align:center">0.7</td>
</tr>
<tr>
<td style="text-align:center">0.3</td>
<td style="text-align:center">0.7</td>
</tr>
<tr>
<td style="text-align:center">1.8</td>
<td style="text-align:center">1.4</td>
</tr>
<tr>
<td style="text-align:center">0.5</td>
<td style="text-align:center">0.6</td>
</tr>
<tr>
<td style="text-align:center">0.3</td>
<td style="text-align:center">0.6</td>
</tr>
<tr>
<td style="text-align:center">2.5</td>
<td style="text-align:center">2.6</td>
</tr>
<tr>
<td style="text-align:center">1.3</td>
<td style="text-align:center">1.1</td>
</tr>
</tbody>
</table>
<p>PCA第一步是用解释变量减去样本均值：</p>
<table>
<thead><tr>
<th style="text-align:center">X1</th>
<th style="text-align:center">X2</th>
</tr></thead>
<tbody>
<tr>
<td style="text-align:center">0.9 - 1.17 = -0.27</td>
<td style="text-align:center">1 - 1.3 = -0.3</td>
</tr>
<tr>
<td style="text-align:center">2.4 - 1.17 = 1.23</td>
<td style="text-align:center">2.6 - 1.3 = 1.3</td>
</tr>
<tr>
<td style="text-align:center">1.2 - 1.17 = 0.03</td>
<td style="text-align:center">1.7 - 1.3 = 0.4</td>
</tr>
<tr>
<td style="text-align:center">0.5 - 1.17 = -0.67</td>
<td style="text-align:center">-0.7 - 1.3 = 0.6</td>
</tr>
<tr>
<td style="text-align:center">0.3 - 1.17 = -0.87</td>
<td style="text-align:center">-0.7 - 1.3 = 0.6</td>
</tr>
<tr>
<td style="text-align:center">1.8 - 1.17 = 0.63</td>
<td style="text-align:center">1.4 - 1.3 = 0.1</td>
</tr>
<tr>
<td style="text-align:center">0.5 - 1.17 = -0.67</td>
<td style="text-align:center">0.6 - 1.3 = -0.7</td>
</tr>
<tr>
<td style="text-align:center">0.3 - 1.17 = -0.87</td>
<td style="text-align:center">0.6 - 1.3 = -0.7</td>
</tr>
<tr>
<td style="text-align:center">2.5 - 1.17 = 1.33</td>
<td style="text-align:center">2.6 - 1.3 = 1.3</td>
</tr>
<tr>
<td style="text-align:center">1.3 - 1.17 = 0.13</td>
<td style="text-align:center">1.1 - 1.3 = -0.2</td>
</tr>
</tbody>
</table>
<p>然后，我们计算数据的主成分。前面介绍过，矩阵的主成分是其协方差矩阵的特征向量，按照对应的特征值大小排序。主成分可以通过两种方法计算。第一种方法是计算数据协方差矩阵。因为协方差矩阵是方阵，所有我们可以用前面的方法计算特征值和特征向量。第二种方法是用数据矩阵的奇异值分解（singular value decomposition）来找协方差矩阵的特征向量和特征值的平方根。我们先介绍第一种方法，然后介绍scikit-learn的PCA实现，也就是第二种方法。上述数据集的解释变量协方差矩阵如下：</p>
$$
C=
\begin{bmatrix}
0.6867777778 &amp; 0.6066666667 \\
0.6066666667 &amp; 0.5977777778 \\
\end{bmatrix}
$$<p>用前面介绍过的方法，特征值是1.250和0.034，单位特征向量是：</p>
$$
\begin{bmatrix}
0.73251454 &amp; 0.68075138 \\
0.68075138 &amp; 0.73251454 \\
\end{bmatrix}
$$<p>下面我们把数据映射到主成分上。第一主成分是最大特征值对应的特征向量，因此我们要建一个转换矩阵，它的每一列都是主成分的特征向量。如果我们要把5维数据降成3维，那么我们就要用一个3维矩阵做转换矩阵。在本例中，我们将把我们的二维数据映射成一维，因此我们只需要用特征向量中的第一主成分。最后，我们用数据矩阵点乘转换矩阵。下面就是第一主成分映射的结果：</p>
$$
\begin{bmatrix}
-0.27 &amp; -0.3 \\
1.23 &amp; 1.3 \\
0.03 &amp; 0.4 \\
-0.67 &amp; 0.6 \\
-0.87 &amp; 0.6 \\
0.63 &amp; 0.1 \\
-0.67 &amp; -0.7 \\
-0.87 &amp; -0.7 \\
1.33 &amp; 1.3 \\
0.13 &amp; -0.2 \\
\end{bmatrix}
\begin{bmatrix}
0.73251454 \\
0.68075138 \\
\end{bmatrix}
=
\begin{bmatrix}
-0.40200434 \\
 1.78596968 \\
 0.29427599 \\
-0.08233391 \\
-0.22883682 \\
 0.5295593  \\
-0.96731071 \\
-1.11381362 \\
 1.85922113 \\
-0.04092339 \\
\end{bmatrix}
$$<p>通过Numpy的<code>dot</code>函数计算如下：</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [12]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="p">[[</span><span class="o">-</span><span class="mf">0.27</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.3</span><span class="p">],</span>
<span class="p">[</span><span class="mf">1.23</span><span class="p">,</span> <span class="mf">1.3</span><span class="p">],</span>
<span class="p">[</span><span class="mf">0.03</span><span class="p">,</span> <span class="mf">0.4</span><span class="p">],</span>
<span class="p">[</span><span class="o">-</span><span class="mf">0.67</span><span class="p">,</span> <span class="mf">0.6</span><span class="p">],</span>
<span class="p">[</span><span class="o">-</span><span class="mf">0.87</span><span class="p">,</span> <span class="mf">0.6</span><span class="p">],</span>
<span class="p">[</span><span class="mf">0.63</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">],</span>
<span class="p">[</span><span class="o">-</span><span class="mf">0.67</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.7</span><span class="p">],</span>
<span class="p">[</span><span class="o">-</span><span class="mf">0.87</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.7</span><span class="p">],</span>
<span class="p">[</span><span class="mf">1.33</span><span class="p">,</span> <span class="mf">1.3</span><span class="p">],</span>
<span class="p">[</span><span class="mf">0.13</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.2</span><span class="p">]]</span>

<span class="n">b</span> <span class="o">=</span> <span class="p">[[</span><span class="mf">0.73251454</span><span class="p">],</span> <span class="p">[</span><span class="mf">0.68075138</span><span class="p">]]</span>

<span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt output_prompt">Out[12]:</div>


<div class="output_text output_subarea output_execute_result">
<pre>array([[-0.40200434],
       [ 1.78596968],
       [ 0.29427599],
       [-0.08233391],
       [-0.22883682],
       [ 0.5295593 ],
       [-0.96731071],
       [-1.11381362],
       [ 1.85922113],
       [-0.04092339]])</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>许多PCA的实现方法，包括scikit-learn的实现方法都是用奇异值分解计算特征值和特征向量。SVD计算公式如下：</p>
$$X=U \sum {V^T}$$<p>列向量$U$称为数据矩阵的左奇异值向量，$V$称为数据矩阵的右奇异值向量，$\sum$的对角线元素是它的奇异值。矩阵的奇异值向量和奇异值在一些信号处理和统计学中是十分有用的，我们只对它们与数据矩阵特征向量和特征值相关的内容感兴趣。具体来说，左奇异值向量就是协方差矩阵的特征向量，$\sum$的对角线元素是协方差矩阵的特征值的平方根。计算SVD超出本书范围，不过用SVD找特征向量的方法与通过协方差矩阵解析方法类似，详细内容见线性代数教程。</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="用PCA实现高维数据可视化">用PCA实现高维数据可视化<a class="anchor-link" href="7-dimensionality-reduction-with-pca.html#%E7%94%A8PCA%E5%AE%9E%E7%8E%B0%E9%AB%98%E7%BB%B4%E6%95%B0%E6%8D%AE%E5%8F%AF%E8%A7%86%E5%8C%96">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>二维或三维数据更容易通过可视化发现模式。一个高维数据集是无法用图形表示的，但是我们可以通过降维方法把它降成二维或三维数据来可视化。</p>
<p>Fisher1936年收集了三种鸢尾花分别50个样本数据（Iris Data）：Setosa、Virginica、Versicolour。解释变量是花瓣（petals）和萼片（sepals）长度和宽度的测量值，响应变量是花的种类。鸢尾花数据集经常用于分类模型测试，scikit-learn中也有。让我们把<code>iris</code>数据集降成方便可视化的二维数据：</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [5]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">%</span><span class="k">matplotlib</span> inline
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="kn">from</span> <span class="nn">sklearn.decomposition</span> <span class="k">import</span> <span class="n">PCA</span>
<span class="kn">from</span> <span class="nn">sklearn.datasets</span> <span class="k">import</span> <span class="n">load_iris</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>首先，我们导入鸢尾花数据集和<code>PCA</code>估计器。<code>PCA</code>类把主成分的数量作为超参数，和其他估计器一样，<code>PCA</code>也用<code>fit_transform()</code>返回降维的数据矩阵：</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [6]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">data</span> <span class="o">=</span> <span class="n">load_iris</span><span class="p">()</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">target</span>
<span class="n">X</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">data</span>
<span class="n">pca</span> <span class="o">=</span> <span class="n">PCA</span><span class="p">(</span><span class="n">n_components</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="n">reduced_X</span> <span class="o">=</span> <span class="n">pca</span><span class="o">.</span><span class="n">fit_transform</span><span class="p">(</span><span class="n">X</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>最后，我们把图形画出来：</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [9]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">red_x</span><span class="p">,</span> <span class="n">red_y</span> <span class="o">=</span> <span class="p">[],</span> <span class="p">[]</span>
<span class="n">blue_x</span><span class="p">,</span> <span class="n">blue_y</span> <span class="o">=</span> <span class="p">[],</span> <span class="p">[]</span>
<span class="n">green_x</span><span class="p">,</span> <span class="n">green_y</span> <span class="o">=</span> <span class="p">[],</span> <span class="p">[]</span>

<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">reduced_X</span><span class="p">)):</span>
    <span class="k">if</span> <span class="n">y</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
        <span class="n">red_x</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">reduced_X</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span>
        <span class="n">red_y</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">reduced_X</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">1</span><span class="p">])</span>
    <span class="k">elif</span> <span class="n">y</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
        <span class="n">blue_x</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">reduced_X</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span>
        <span class="n">blue_y</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">reduced_X</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">1</span><span class="p">])</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">green_x</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">reduced_X</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span>
        <span class="n">green_y</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">reduced_X</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">1</span><span class="p">])</span>

<span class="n">plt</span><span class="o">.</span><span class="n">scatter</span><span class="p">(</span><span class="n">red_x</span><span class="p">,</span> <span class="n">red_y</span><span class="p">,</span> <span class="n">c</span><span class="o">=</span><span class="s1">'r'</span><span class="p">,</span> <span class="n">marker</span><span class="o">=</span><span class="s1">'x'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">scatter</span><span class="p">(</span><span class="n">blue_x</span><span class="p">,</span> <span class="n">blue_y</span><span class="p">,</span> <span class="n">c</span><span class="o">=</span><span class="s1">'b'</span><span class="p">,</span> <span class="n">marker</span><span class="o">=</span><span class="s1">'D'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">scatter</span><span class="p">(</span><span class="n">green_x</span><span class="p">,</span> <span class="n">green_y</span><span class="p">,</span> <span class="n">c</span><span class="o">=</span><span class="s1">'g'</span><span class="p">,</span> <span class="n">marker</span><span class="o">=</span><span class="s1">'.'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>


<div class="output_png output_subarea ">
<img src="%0AAAALEgAACxIB0t1+/AAAIABJREFUeJzt3X2wZHWd3/H3h7n3RkeGdfEBFrnJJRWo6K6KmCIMhuXi%0ArhZiBEFRqVp20SnZXWWwWCpRVGTiaM3uJq4bRyYh7viYLBhWFpkFS9mVq1RBIMiDrDIiZd1k8AGM%0AKDM6wJ0L3/xxTk8/3H443ed0n9Pdn1fVKfrhnD4/Bubbv/7+fr/vTxGBmZlNrkPKboCZmQ2XA72Z%0A2YRzoDczm3AO9GZmE86B3sxswjnQm5lNuNyBXtKnJT0i6f4O7y9KelzSPenxwbz3NDOz7GYK+IzP%0AANuBz3c55xsRcWYB9zIzsz7l7tFHxK3Az3ucprz3MTOzwYwiRx/AyZLuk3STpJeM4J5mZpYqInXT%0Ay93AfETsl/Q64HrguBHc18zMGEGgj4h9DY+/ImmHpMMj4rHG8yS56I6Z2QAiomt6fOiBXtIRwKMR%0AEZJOBNQa5Gt6NXbUJG2JiC1lt6OR25RdFdvlNmXjNmWXpZOcO9BLuho4FXi+pD3AFcAsQERcBbwZ%0A+GNJq8B+4G1572lmZtnlDvQRcV6P968Ersx7HzMzG4xXxna3VHYD2lgquwFtLJXdgA6Wym5AG0tl%0AN6CNpbIb0MZS2Q1oY6nsBgxKVdl4RFJULUdvZlZ1WWKne/RmZhPOgd7MbMI50JuZTTgHejOzCedA%0Ab2Y24RzorTKkufOkua7rMsysf6MoambWkzS7CQ7bnj5eH3FgZ9ltMpsUDvRWunqQv/3ZySsbt0uz%0AONibFcMLpqxUSapmw84kyNeqVz8IbHwC9m2KWLm6zPaZVZ0XTJmZmXv0Vr42qZsnYO9mp27MessS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VTtfwUsldiUk4EzJZ0BPAs4TNLnI+L3S2wTEfHj9J8/%0AlfS3JGnLtoG+EqmbiPjHiDgiIo6JiGNI/mKeMIog34ukYxuengXcU1ZbahoWq53VZbFamcrMYR5c%0ApCdpjmSR3g0ltqeylPSqdgLfjYi/LLs9AJKeL+m56eNnk0zOKPXvXES8PyLm09j0NuDrZQd5Sesl%0AbUgfP4ckG9JxRlclAn0bVfr5vU3S/WnOcBG4tOT2QIfFamWSdLakPSSzN26U9JUy2lHVRXqSrgZu%0AA46TtEfS0NN/GbwK+D2SmS33pEfZM4N+A/h6+vftDmBXRPxDyW1qVYX4dARwa8Of099FxNc6newF%0AU2ZmE66qPXozMyuIA72Z2YRzoDczm3AO9GZmE86B3sxswjnQm5lNOAd6M7MJ50BvZjbh/j/+WMfn%0AZw7rZAAAAABJRU5ErkJggg==">
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>降维的数据如上图所示。每个数据集中三个类都用不同的符号标记。从这个二维数据图中可以明显看出，有一个类与其他两个重叠的类完全分离。这个结果可以帮助我们选择分类模型。</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="PCA脸部识别">PCA脸部识别<a class="anchor-link" href="7-dimensionality-reduction-with-pca.html#PCA%E8%84%B8%E9%83%A8%E8%AF%86%E5%88%AB">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>现在让我们用PCA来解决一个脸部识别问题。脸部识别是一个监督分类任务，用于从照片中认出某个人。本例中，我们用剑桥大学AT&amp;T实验室的<a href="http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html">Our Database of Faces数据集</a>，这个数据集包含40个人每个人10张照片。这些照片是在不同的光照条件下拍摄的，每张照片的表情也不同。照片都是黑白的，尺寸为92 x 112像素。虽然这些图片都不大，但是每张图片的按像素强度排列的特征向量也有10304维。这些高维数据的训练可能需要很多样本才能避免拟合过度。而我们样本量并不大，所有我们用PCA计算一些主成分来表示这些照片。</p>
<p>我们可以把照片的像素强度矩阵转换成向量，然后用所有的训练照片的向量建一个矩阵。每个照片都是数据集主成分的线性组合。在脸部识别理论中，这些主成分称为特征脸（eigenfaces）。特征脸可以看成是脸部的标准化组成部分。数据集中的每张脸都可以通过一些标准脸的组合生成出来，或者说是最重要的特征脸线性组合的近似值。</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [1]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">os</span> <span class="k">import</span> <span class="n">walk</span><span class="p">,</span> <span class="n">path</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">mahotas</span> <span class="k">as</span> <span class="nn">mh</span>
<span class="kn">from</span> <span class="nn">sklearn.cross_validation</span> <span class="k">import</span> <span class="n">train_test_split</span>
<span class="kn">from</span> <span class="nn">sklearn.cross_validation</span> <span class="k">import</span> <span class="n">cross_val_score</span>
<span class="kn">from</span> <span class="nn">sklearn.preprocessing</span> <span class="k">import</span> <span class="n">scale</span>
<span class="kn">from</span> <span class="nn">sklearn.decomposition</span> <span class="k">import</span> <span class="n">PCA</span>
<span class="kn">from</span> <span class="nn">sklearn.linear_model</span> <span class="k">import</span> <span class="n">LogisticRegression</span>
<span class="kn">from</span> <span class="nn">sklearn.metrics</span> <span class="k">import</span> <span class="n">classification_report</span>

<span class="n">X</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">y</span> <span class="o">=</span> <span class="p">[]</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>下面我们把照片导入<code>Numpy</code>数组，然后把它们的像素矩阵转换成向量：</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [6]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">for</span> <span class="n">dir_path</span><span class="p">,</span> <span class="n">dir_names</span><span class="p">,</span> <span class="n">file_names</span> <span class="ow">in</span> <span class="n">walk</span><span class="p">(</span><span class="s1">'mlslpic/att-faces/'</span><span class="p">):</span>
    <span class="k">for</span> <span class="n">fn</span> <span class="ow">in</span> <span class="n">file_names</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">fn</span><span class="p">[</span><span class="o">-</span><span class="mi">3</span><span class="p">:]</span> <span class="o">==</span> <span class="s1">'pgm'</span><span class="p">:</span>
            <span class="n">image_filename</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">dir_path</span><span class="p">,</span> <span class="n">fn</span><span class="p">)</span>
            <span class="n">X</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">scale</span><span class="p">(</span><span class="n">mh</span><span class="o">.</span><span class="n">imread</span><span class="p">(</span><span class="n">image_filename</span><span class="p">,</span> <span class="n">as_grey</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">10304</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">'float32'</span><span class="p">)))</span>
            <span class="n">y</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dir_path</span><span class="p">)</span>

<span class="n">X</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">X</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>然后，我们用交叉检验建立训练集和测试集，在训练集上用<code>PCA：</code></p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [3]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">X_train</span><span class="p">,</span> <span class="n">X_test</span><span class="p">,</span> <span class="n">y_train</span><span class="p">,</span> <span class="n">y_test</span> <span class="o">=</span> <span class="n">train_test_split</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="n">pca</span> <span class="o">=</span> <span class="n">PCA</span><span class="p">(</span><span class="n">n_components</span><span class="o">=</span><span class="mi">150</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>我们把所有样本降到150维，然后训练一个逻辑回归分类器。数据集包括40个类；scikit-learn底层会自动用one versus all策略创建二元分类器：</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [4]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">X_train_reduced</span> <span class="o">=</span> <span class="n">pca</span><span class="o">.</span><span class="n">fit_transform</span><span class="p">(</span><span class="n">X_train</span><span class="p">)</span>
<span class="n">X_test_reduced</span> <span class="o">=</span> <span class="n">pca</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">X_test</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">'训练集数据的原始维度是：</span><span class="si">{}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">X_train</span><span class="o">.</span><span class="n">shape</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">'PCA降维后训练集数据是：</span><span class="si">{}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">X_train_reduced</span><span class="o">.</span><span class="n">shape</span><span class="p">))</span>
<span class="n">classifier</span> <span class="o">=</span> <span class="n">LogisticRegression</span><span class="p">()</span>
<span class="n">accuracies</span> <span class="o">=</span> <span class="n">cross_val_score</span><span class="p">(</span><span class="n">classifier</span><span class="p">,</span> <span class="n">X_train_reduced</span><span class="p">,</span> <span class="n">y_train</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>训练集数据的原始维度是：(300, 10304)
PCA降维后训练集数据是：(300, 150)
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>最后，我们用交叉验证和测试集评估分类器的性能。分类器的平均综合评价指标（F1 score）是0.88，但是需要花费更多的时间训练，在更多训练实例的应用中可能会更慢。</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [7]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="s1">'交叉验证准确率是：</span><span class="si">{}</span><span class="se">\n</span><span class="si">{}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">accuracies</span><span class="p">),</span> <span class="n">accuracies</span><span class="p">))</span>
<span class="n">classifier</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">X_train_reduced</span><span class="p">,</span> <span class="n">y_train</span><span class="p">)</span>
<span class="n">predictions</span> <span class="o">=</span> <span class="n">classifier</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">X_test_reduced</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">classification_report</span><span class="p">(</span><span class="n">y_test</span><span class="p">,</span> <span class="n">predictions</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>交叉验证准确率是：0.823104855161
[ 0.84210526  0.79        0.8372093 ]
             precision    recall  f1-score   support

mlslpic/att-faces/s1       1.00      1.00      1.00         1
mlslpic/att-faces/s10       1.00      1.00      1.00         2
mlslpic/att-faces/s11       1.00      0.83      0.91         6
mlslpic/att-faces/s12       1.00      1.00      1.00         2
mlslpic/att-faces/s13       1.00      1.00      1.00         3
mlslpic/att-faces/s14       0.33      1.00      0.50         2
mlslpic/att-faces/s15       1.00      1.00      1.00         4
mlslpic/att-faces/s17       1.00      1.00      1.00         2
mlslpic/att-faces/s18       1.00      1.00      1.00         2
mlslpic/att-faces/s19       1.00      1.00      1.00         2
mlslpic/att-faces/s2       0.00      0.00      0.00         0
mlslpic/att-faces/s20       1.00      1.00      1.00         2
mlslpic/att-faces/s21       1.00      1.00      1.00         3
mlslpic/att-faces/s22       1.00      1.00      1.00         3
mlslpic/att-faces/s23       1.00      1.00      1.00         1
mlslpic/att-faces/s24       1.00      1.00      1.00         3
mlslpic/att-faces/s25       1.00      1.00      1.00         4
mlslpic/att-faces/s26       1.00      1.00      1.00         4
mlslpic/att-faces/s27       1.00      1.00      1.00         3
mlslpic/att-faces/s28       0.00      0.00      0.00         1
mlslpic/att-faces/s29       1.00      0.50      0.67         2
mlslpic/att-faces/s3       1.00      1.00      1.00         3
mlslpic/att-faces/s30       1.00      1.00      1.00         3
mlslpic/att-faces/s31       0.75      1.00      0.86         3
mlslpic/att-faces/s32       1.00      0.75      0.86         4
mlslpic/att-faces/s33       0.00      0.00      0.00         1
mlslpic/att-faces/s34       0.75      1.00      0.86         3
mlslpic/att-faces/s35       1.00      1.00      1.00         2
mlslpic/att-faces/s36       0.50      1.00      0.67         1
mlslpic/att-faces/s37       1.00      0.17      0.29         6
mlslpic/att-faces/s38       1.00      1.00      1.00         2
mlslpic/att-faces/s39       1.00      1.00      1.00         2
mlslpic/att-faces/s4       1.00      1.00      1.00         1
mlslpic/att-faces/s40       0.00      0.00      0.00         1
mlslpic/att-faces/s5       0.80      0.80      0.80         5
mlslpic/att-faces/s6       1.00      1.00      1.00         2
mlslpic/att-faces/s7       1.00      1.00      1.00         2
mlslpic/att-faces/s8       1.00      1.00      1.00         4
mlslpic/att-faces/s9       1.00      1.00      1.00         3

avg / total       0.93      0.88      0.88       100

</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="总结">总结<a class="anchor-link" href="7-dimensionality-reduction-with-pca.html#%E6%80%BB%E7%BB%93">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>本章，我们介绍了降维问题。高维数据不能轻易可视化。估计器训练高维数据集时，也可能出现维度灾难。我们通过主成分分析法缓解这些问题，将可能解释变量具有相关性的高维数据集，通过将数据映射到一个低维子空间，降维成一个线性无关的低维数据集。我们用主成分分析将四维的鸢尾花数据集降成二维数据进行可视化，还建立了一个脸部识别系统。下一章，我们将回到监督学习方法，介绍一种分类算法——感知器（perceptron），本书的最后两章都是建立在感知器的基础上。</p>

</div>
</div>
</div>
    </div>
  </div>

    </div>
    <aside class="postpromonav"><nav><ul itemprop="keywords" class="tags">
<li><a class="tag p-category" href="../categories/chs.html" rel="tag">CHS</a></li>
            <li><a class="tag p-category" href="../categories/ipython.html" rel="tag">ipython</a></li>
            <li><a class="tag p-category" href="../categories/machine-learning.html" rel="tag">Machine Learning</a></li>
            <li><a class="tag p-category" href="../categories/python.html" rel="tag">Python</a></li>
            <li><a class="tag p-category" href="../categories/scikit-learn-tutorial.html" rel="tag">scikit-learn tutorial</a></li>
        </ul>
<ul class="pager hidden-print">
<li class="previous">
                <a href="dlott-15075-2015-07-01-report.html" rel="prev" title="大乐透15075期(2015-07-01)数据分析报告">Previous post</a>
            </li>
            <li class="next">
                <a href="slott-2015076-2015-07-02-report.html" rel="next" title="双色球2015076期(2015-07-02)数据分析报告">Next post</a>
            </li>
        </ul></nav></aside><script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"> </script><script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"] ],
        displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
        processEscapes: true
    },
    displayAlign: 'center', // Change this to 'center' to center equations.
    "HTML-CSS": {
        styles: {'.MathJax_Display': {"margin": 0}}
    }
});
</script></article>
</div>
        <!--End of body content-->

        <footer id="footer">
            Contents © 2017         <a href="mailto:muxuezi@gmail.com">Tao Junjie</a> - Powered by         <a href="https://getnikola.com" rel="nofollow">Nikola</a>         
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0">
<img alt="Creative Commons License BY-NC-SA" style="border-width:0; margin-bottom:12px;" src="http://i.creativecommons.org/l/by-nc-sa/4.0/80x15.png"></a>
            
        </footer>
</div>
</div>


            <script src="../assets/js/all-nocdn.js"></script><script>$('a.image-reference:not(.islink) img:not(.islink)').parent().colorbox({rel:"gal",maxWidth:"100%",maxHeight:"100%",scalePhotos:true});</script><!-- fancy dates --><script>
    moment.locale("en");
    fancydates(0, "YYYY-MM-DD HH:mm");
    </script><!-- end fancy dates --><script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-51330059-1', 'auto');
  ga('send', 'pageview');

</script>
</body>
</html>
